/*
 * @Author: CPS
 * @Date:   2020-08-05 16:46:40
 * @Last Modified by: CPS
 * @Last Modified time: 2020-08-29 12:23:38
 */
'use strict'
import fs from 'fs'
import path from 'path'

import readDocx from './readDocx.js'
import {
  getFolderSize,
  getFileSize
} from './getFolderSize.js'

// 对应不同后缀指定处理函数
let getExtInfo = {
  docx: readDocx,
  doc: readDocx,
}

/**
 * 分别对文件夹和文件今夕信息获取,并封装成数组返回
 *
 * @param      {<type>}  absPath  The abs path
 */
function getFilesInfo(absPath) {
  let fileList = fs.readdirSync(absPath);
  let files = []
  fileList.forEach((item) => {
    let tar = path.resolve(absPath, item)
    let res
    if (fs.lstatSync(tar).isFile()) {
      res = getFileInfo(tar)
    } else {
      res = getFolderInfo(tar)
    }

    files.push(res)
  })

  return files
}

/**
 * 传入目录的绝对路径,返回目录相关信息
 * (对应结构 folders[xxx][files][xxxxx])
 *
 * @param      {<type>}  tar     绝对路径
 */
function getFolderInfo(absPath) {
  //文件夹信息记录
  return {
    type: 'folder',
    basename: path.basename(absPath),
    fullname: path.basename(absPath),
    fullPath: absPath,
    size: getFolderSize(absPath),
    filecount: fs.readdirSync(absPath).length ? fs.readdirSync(absPath).length : 0,
  }
}


/**
 * Gets the file information.
 * (对应结构 folders[xxx][files][xxxxx])
 *
 * @param      {<type>}  absPath  The abs path
 */
function getFileInfo(absPath) {
  //读取基本信息
  let fstat = fs.lstatSync(absPath)

  //获取文件格式
  let ext = path.extname(absPath).toLowerCase()

  let fileInfo = {
    type: 'file',
    basename: path.basename(absPath, ext),
    fullname: path.basename(absPath),
    fullPath: absPath,
    size: getFileSize(fstat.size),
    ext: ext.replace('.', ''),
    pages: 0,
    words: 0,
    lines: 0,
  }

  //使用getExtInfo函数，获取文件的 页数 行数 字数 大小 信息
  if (getExtInfo.hasOwnProperty(fileInfo['ext'])) {
    Object.assign(fileInfo, getExtInfo[fileInfo['ext']](fileInfo['fullPath']))
  }

  // 合并结果
  return fileInfo
}


/**
 * 读取目录下各种格式的数量,返回对象
 * (对应结构 folders[xxx].filecount)
 *
 * @param      {<type>}  absPath  一个目录路径
 */
function getFolderCount(absPath) {
  let fileList = fs.readdirSync(absPath)
  let tarExt
  let res = {}
  fileList.forEach((item) => {
    let ext = path.extname(item).replace('.', '')
    if (!ext) return

    if (ext == 'doc' || ext == 'docx') {
      tarExt = 'word'
    } else if (ext == 'xls' || ext == 'xlsx') {
      tarExt = 'excel'
    } else {
      tarExt = ext
    }

    // 没有的格式进行归0初始化
    if (!res.hasOwnProperty(tarExt)) res[tarExt] = 0
    res[tarExt] += 1
  })
  return res
}

/**
 * 每个文件夹的大概信息
 * (对应结构 floders[xxxx] 对象)
 *
 * @param      {<type>}  absPath  The abs path
 */
function readSingleFolder(absPath) {
  // 创建目录结构
  return {
    filelist: fs.readdirSync(absPath),
    dirname: path.basename(absPath),
    dirPath: path.resolve(path.dirname(absPath)),
    fullPath: absPath,
    filecount: getFolderCount(absPath),
    files: getFilesInfo(absPath),
  }
}

/**
 * 每个文件夹的结构信息
 * (对应结构 floders[xxxx] 对象)
 *
 * @param      {<type>}  absPath  The abs path
 */
function readEachFolder(absPath) {
  // 创建目录结构
  let folders = {}
  let fileList = fs.readdirSync(absPath)

  fileList.forEach((item) => {
    let dirPath = path.resolve(absPath, item)
    if (!fs.lstatSync(dirPath).isFile()) {
      folders[item] = {
        filelist: fs.readdirSync(dirPath),
        dirname: path.basename(dirPath),
        dirPath: path.resolve(path.dirname(dirPath)),
        fullPath: dirPath,
        filecount: getFolderCount(dirPath),
        files: getFilesInfo(dirPath),
      }
    }
  })

  return folders
}

function readMainFolder(absPath) {
  return {
    el: '',
    dirPath: path.resolve(path.dirname(absPath)),
    fullPath: absPath,
    filelist: fs.readdirSync(absPath),
    dirname: path.basename(absPath),
  }
}

/* 返回文件夹的对象 详细信息 */
// 返回格式:
// {
//    folder:{} 主文件夹详细信息
//    folders:{} 子目录详细信息
// }
function createFolderObject(absPath = path.resolve('./')) {
  let folder = readMainFolder(absPath)
  let folders = readEachFolder(absPath)
  return {
    folder: folder,
    folders: folders
  }
}

export {
  getFilesInfo,
  readSingleFolder,
  createFolderObject
}